//
//  WBComposeTextView.swift
//  XLWB
//
//  Created by MacBook on 16/7/12.
//  Copyright © 2016年 MacBook. All rights reserved.
//

import UIKit

class WBComposeTextView: UITextView {

    //占位标签
    private lazy var placholderLabel = UILabel()
    
    override func awakeFromNib() {
        
        setupUI()
    }
    deinit {
        
        NotificationCenter.default.removeObserver(self)
    }

    // MARK: - 监听方法
    @objc private func textChanged(n: Notification) {
        // 如果有文本，不显示占位标签，否则显示
        placholderLabel.isHidden = self.hasText()
    }

}
private extension WBComposeTextView {
    
    func setupUI() {
        
        //注册通知
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(textChanged),
            name: NSNotification.Name.UITextViewTextDidChange,
            object: self)

        
        // 1. 设置占位标签
        placholderLabel.text = "分享新鲜事..."
        placholderLabel.font = self.font
        placholderLabel.textColor = UIColor.lightGray()
        placholderLabel.frame.origin = CGPoint(x: 5, y: 8)
        
        placholderLabel.sizeToFit()
        
        addSubview(placholderLabel)
    }
}
